import os
from django import http
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp

from models import Greeting

def index(request):
    r = http.HttpResponse('<h1>Django examples</h1><ul>')
    
    greetings_query = Greeting.all().order('-date')
    greetings = greetings_query.fetch(10)

    if users.get_current_user():
      url = users.create_logout_url('http://' + request.META['HTTP_HOST'] + request.get_full_path())
      url_linktext = 'Logout'
    else:
      url = users.create_login_url('http://' + request.META['HTTP_HOST'] + request.get_full_path())
      url_linktext = 'Login'

    template_values = {
      'greetings': greetings,
      'url': url,
      'url_linktext': url_linktext,
      }
    
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    r.write(template.render(path, template_values))
    
    return r
